﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Linq;
using ReactiveUI;
using System.Windows.Input;
using Avalonia.MusicStore.Models;
using System.Reactive.Concurrency;
using System.Runtime.InteropServices;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using MsBox.Avalonia;
using MsBox.Avalonia.Dto;
using MsBox.Avalonia.Enums;
using MsBox.Avalonia.Models;

namespace Avalonia.MusicStore.ViewModels;

public partial class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
        BuyMusicCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            var store = new MusicStoreViewModel();
            var result = await ShowDialog.Handle(store);
            if (result != null)
            {
                Albums.Add(result);
                await result.SaveToDiskAsync();
            }
        });

        TestCommand = ReactiveCommand.Create(async () =>
        {
            // var box = MessageBoxManager.GetMessageBoxStandard("提示", "确定执行此操作吗？", ButtonEnum.YesNo, Icon.None, WindowStartupLocation.CenterOwner);
            var filename = System.IO.Path.Combine(Environment.CurrentDirectory, "Assets", "avalonia-logo.ico");
            var box = MessageBoxManager.GetMessageBoxCustom(new MessageBoxCustomParams
            {
                ButtonDefinitions =
                [
                    new ButtonDefinition { Name = "取消", IsCancel = true },
                    new ButtonDefinition { Name = "确定" },
                ],
                ContentTitle = "提示",
                ContentMessage = "我是弹窗",
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                Icon = Icon.Question,
                CanResize = false,
                MaxWidth = 500,
                MaxHeight = 800,
                SizeToContent = SizeToContent.WidthAndHeight,
                ShowInCenter = true,
                Topmost = false,
                HyperLinkParams = new HyperLinkParams
                {
                    Text = "进入网页",
                    Action = () =>
                    {
                        var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                        var url = "https://i.whliupangzi.cn";
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            using var proc = new Process();
                            proc.StartInfo.UseShellExecute = true;
                            proc.StartInfo.FileName = url;
                            proc.Start();
                            return;
                        }

                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                        {
                            Process.Start("x-www-browser", url);
                            return;
                        }

                        if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                            throw new Exception("invalid url: " + url);
                        Process.Start("open", url);
                    }
                }
            });
            var result = await box.ShowAsync();
            return new object();
        });

        RxApp.MainThreadScheduler.Schedule(LoadAlbums);
    }

    public Interaction<MusicStoreViewModel, AlbumViewModel> ShowDialog { get; } = new();

    public ObservableCollection<AlbumViewModel> Albums { get; } = [];

    public ICommand BuyMusicCommand { get; }

    public ICommand TestCommand { get; }

    private async void LoadAlbums()
    {
        var albums = (await Album.LoadCachedAsync()).Select(x => new AlbumViewModel(x));

        foreach (var album in albums)
        {
            Albums.Add(album);
        }

        foreach (var album in Albums.ToList())
        {
            await album.LoadCover();
        }
    }
}